package com.kobe.game_40;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kobe.util.Perm;

/**
 * 
 * We shall say that an n -digit number is pandigital if it makes use of all the
 * digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1
 * through 5 pandigital.
 * 
 * The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing
 * multiplicand, multiplier, and product is 1 through 9 pandigital.
 * 
 * Find the sum of all products whose multiplicand/multiplier/product identity
 * can be written as a 1 through 9 pandigital. HINT: Some products can be
 * obtained in more than one way so be sure to only include it once in your sum.
 * 
 */
public class Game32 {
    public static void main(String[] args) {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        List<int[]> permedNumberList = Perm.getPerm(numbers);
        int[] tempPermedNumbers;
        Set<Integer> resultSet = new HashSet<Integer>();
        for (int i = 0; i < permedNumberList.size(); i++) {
            tempPermedNumbers = permedNumberList.get(i);
            int a = tempPermedNumbers[0] + tempPermedNumbers[1] * 10;
            int b = tempPermedNumbers[2] + tempPermedNumbers[3] * 10
                    + tempPermedNumbers[4] * 100;
            int x = tempPermedNumbers[5] + tempPermedNumbers[6] * 10
                    + tempPermedNumbers[7] * 100 + tempPermedNumbers[8] * 1000;
            if (a * b == x) {
                resultSet.add(x);
            }
            int c = tempPermedNumbers[0];
            int d = tempPermedNumbers[1] + tempPermedNumbers[2] * 10
                    + tempPermedNumbers[3] * 100 + tempPermedNumbers[4] * 1000;
            if (c * d == x) {
                resultSet.add(x);
            }
        }

        int result = 0;
        for (int temp : resultSet) {
            result += temp;
        }

        System.out.println(result);
    }
}
